<template>
	<view style="">
		<view class="all_project_head_btn" style="">
			<project-button :projectId="project_id" stageName="工作计划"></project-button>
			<step-button :approvalLog="addFormData.approval_log" :processList="stepProcessList"
				v-if="addFormData.approval_log != null"></step-button>
		</view>
		<view style="padding-bottom: 200rpx;border: 0;">
			<!-- 项目名称 -->
			<view class="info_item" style="border: 0;">
				<text class="all_text_title">项目名称</text>
				<textarea disabled v-model="S_project_name" maxlength="-1"
				:class="true?'all_new_input_disable_style':'all_new_input_style'"
					auto-height="true" style="width: 95%;padding: 18rpx;"
					 />
			</view>


			<!-- 时间安排 -->
			<view class="info_item">
				<view style="display: flex;flex-direction: row;">
					<text class="all_text_title">时间安排</text>
					<view v-if="isSubmit" class="add_user" @click="addUser()">添加安排</view>
				</view>
				<view class="line1">
					<view class="column1">工作内容</view>
					<view class="column2">开始时间</view>
					<view class="column2">完成时间</view>
					<view class="column3">操作</view>
				</view>
				<view class="line2" v-for="(item, index) in work_info" :id="item.id">
					<view class="column1">
						<input :disabled="!isSubmit" type="text" v-model="item.workInfo" />
					</view>
					<view class="column2">
						<picker mode="date" @change="e => setDate1(e, item, index)" :disabled="!isSubmit">
							<view class="piker_date">{{ item.startTime }}</view>
						</picker>
					</view>
					<view class="column2">
						<picker mode="date" @change="e => setDate2(e, item, index)" :disabled="!isSubmit">
							<view class="piker_date">{{ item.endTime }}</view>
						</picker>
					</view>
					<view class="column3" style="color: #128BF1;" :style="!isSubmit?'color: #ffffff;':'color: #128BF1;'" @click="deleteUser(index)">
						删除
					</view>
				</view>
			</view>
			
			<!-- 附件上传提示 -->
			<view class="info_item" style="border-bottom: 0; color: #40A0F2;font-size: 26rpx;" @click="getMuBan()">
				<text style="color: #FF5A5F;">*请上传</text>
				<text style="color: #40A0F2;">安全评价工作计划</text>
			</view>
			<!-- 附件上传 -->
			<view class="info_item" style="border: 0;">
				<upload-file
					:isSubmit="isSubmit"
					:imgList="addFormData.annex"
					:detailsImgList="detailsImgList"
					@my-upload-change="upLoadChange"
				></upload-file>
			</view>
			<!-- 审批备注 -->
			<view
				class="info_item"
				v-if="addFormData.is_approve == 1 && details_project_status == '15'"
				style="border-bottom: 0;"
			>
				<text class="all_text_title">审批备注</text>
				<textarea
					v-model="approval_remark"
					class="texta"
					maxlength="-1"
					auto-height="true"
				/>
			</view>
		</view>

	

		<!-- 底部按钮 -->
		<view class="bottom">
			<view v-if="addFormData.is_edit != '1'">
				<button
					@click="handleSubmit('1')"
					class="bottom_btn"
					style="background: #CCCCCC; opacity: 1; width: 380rpx;"
				>
					保存草稿
				</button>
				<button
					@click="handleSubmit('0')"
					class="bottom_btn"
					style="background: linear-gradient(0deg, #037CE3, #2EB3FF); margin-top: -88rpx;margin-left: 50%;"
					form-type="submit"
				>
					提交
				</button>
			</view>
			
			<!-- 可撤回时显示 -->
			<view class="bottom" v-if="addFormData.is_edit == 1 && addFormData.withdraw_status == 1 && addFormData.is_approve != 1">
				<button
					class="bottom_btn"
					@click="handleWithdraw()"
					style="background: #DD524D; width: 100%;"
					form-type="reset"
				>
					撤回
				</button>
			</view>
			
			<view v-if="addFormData.is_approve == 1 && details_project_status == '15' && addFormData.withdraw_status != 1">
				<button
					@click="handleExamine('-1')"
					class="bottom_btn"
					style="background: #CCCCCC; opacity: 1; width: 380rpx;"
				>
					驳回
				</button>
				<button
					class="bottom_btn"
					@click="handleExamine('1')"
					style="background: linear-gradient(0deg, #037CE3, #2EB3FF); margin-top: -88rpx;margin-left: 50%;"
					form-type="submit"
				>
					审批通过
				</button>
			</view>
			
			<!-- 可撤回时显示 -->
			<view class="bottom2" v-if="addFormData.is_approve == 1 && addFormData.withdraw_status == 1">
				<button
					class="bottom_btn2"
					@click="handleWithdraw()"
					style="background: #FF5A5F; margin-left: 0;"
					form-type="reset"
				>
					撤回
				</button>
				<button
					class="bottom_btn2"
					@click="handleExamine('-1')"
					style="background: #CCCCCC;margin-left: 33%;"
					form-type="reset"
				>
					驳回
				</button>
				<button
					class="bottom_btn2"
					style="background: linear-gradient(0deg, #037CE3, #2EB3FF);margin-left: 66%;"
					@click="handleExamine('1')"
					form-type="submit"
				>
					审批通过
				</button>
			</view>
			
		</view>
	</view>
</template>

<script>
import xiaolu from '@/components/xiaolu-signature/index.vue';
import UploadFile from '@/components/upload-file.vue';
import ProjectButton from '@/components/project-button.vue';
import StepButton from '@/components/step-button.vue';

export default {
	components: {
		UploadFile,
		ProjectButton,
		StepButton,
		xiaolu
	},
	data() {
		return {
			stepProcessList:{
					stage_code:'',
					project_id:''
					},
			date: '',
			work_info: [],
			
			addFormData:{},
			S_project_name:'',
			isSubmit: true,
			detailsImgList: [],
			submitImgList: [],
			approval_remark: '',
			details_project_status: '',
			token: '',
			member_id: '',
			company_id: '',
			project_id: '',
		};
	},
	async onLoad(options) {
		this.token = uni.getStorageSync('btlh_process_token') || '';
		this.member_id = uni.getStorageSync('btlh_process_member_id') || '';
		this.company_id = uni.getStorageSync('bthl_process_LoginCompanyId') || '';

		this.details_project_status = options.details_project_status || '';
		this.project_id = options.project_id || '';
		if (this.project_id) {
			this.getProjectDetails();
		}
	},
	// 开启微信发送给朋友
	//#ifdef MP-WEIXIN
	onShareAppMessage() {
		return{
			title:this.addFormData.project_name,
			path:'/packageProject/pages/project/projectPlan?project_id='+this.project_id+'&details_project_status='+this.details_project_status
		}
	},
	//#endif
	created() {
		this.token = uni.getStorageSync('btlh_process_token') || ''
		this.member_id = uni.getStorageSync('btlh_process_member_id') || ''
		// console.log('文件上传组件的merid',this.member_id)
		this.company_id = uni.getStorageSync('bthl_process_LoginCompanyId') || ''
	},
	methods: {
		// 点击复制模板链接
		getMuBan(){
			uni.setClipboardData({
			    data: 'http://control.sxpz.vip/Template/安全评价工作计划.doc',
			    success: function () {
			        uni.showToast({
			        	title:'复制成功'
			        })
			    }
			});
		},
		// 点击撤回
		async handleWithdraw(){
			const { data: res } = await this.$myRequest({
				url: 'Project/withdraw',
				method: 'GET',
				data: {
					member_id: this.member_id,
					token: this.token,
					company_id: this.company_id,
					project_id:this.project_id
				}
			});
			if (res.code != 200)
				return uni.showToast({
					title: res.datas.msg,
					icon: 'none'
				});
				uni.navigateBack({
					delta:1
				})
		},
		// 审批通过
		async handleExamine(val) {
			const { data: res } = await this.$myRequest({
				url: 'Project/scheme_approval',
				method: 'POST',
				data: {
					member_id: this.member_id,
					token: this.token,
					company_id: this.company_id,

					project_id: this.project_id,
					approval_status: val,
					remark: this.approval_remark
				}
			});
			if (res.code != 200)
				return uni.showToast({
					title: res.datas.msg,
					icon: 'none'
				});
			uni.showToast({
				title: res.datas.msg,
				icon: 'none'
			});
			/* uni.navigateTo({
				url: `/packageProject/pages/project/projectManagement`
			}); */
			uni.navigateBack({
				delta: 1
			})
		},
		// 编辑提交
		async handleSubmit(val) {
			const { data: res } = await this.$myRequest({
				url: 'Project/scheme',
				method: 'POST',
				data: {
					member_id: this.member_id,
					token: this.token,
					company_id: this.company_id,
					project_id: this.project_id,
			
					is_draft: val,
			
					annex: JSON.stringify(this.submitImgList),
					schedule: JSON.stringify(this.work_info),
				}
			});
			if (res.code != 200)
				return uni.showToast({
					title: res.datas.msg,
					icon: 'none'
				});
			uni.showToast({
				title: res.datas.msg,
				icon: 'none'
			});
			/* uni.navigateTo({
				url: `/packageProject/pages/project/projectManagement`
			}); */
			uni.navigateBack({
				delta: 1
			})
		},
		// 获取详情
		async getProjectDetails() {
			const { data: res } = await this.$myRequest({
				url: 'Project/scheme_detail',
				method: 'POST',
				data: {
					member_id: this.member_id,
					token: this.token,
					company_id: this.company_id,

					project_id: this.project_id
				}
			});
			if (res.code != 200)
				return uni.showToast({
					title: res.datas.msg,
					icon: 'none'
				});
			// 判断是否首次进入次流程
			if (res.datas.info.is_new != 1) {
				this.addFormData = res.datas.info;
			}else{
				this.addFormData.is_edit = res.datas.info.is_edit
			}
			// 判断禁用
			if (this.addFormData.is_draft != '1' && this.addFormData.is_edit == '1') {
				this.isSubmit = false;
			}
			// 禁用值
			this.S_project_name = res.datas.info.project_name;
			this.stepProcessList.project_id = this.addFormData.project_id;
			this.stepProcessList.stage_code = this.addFormData.project_status;

			// 处理文件
			if (this.addFormData.annex) {
				this.detailsImgList = this.addFormData.annex;
				this.submitImgList = this.addFormData.annex;
			}
			
			// 处理表格
			if (this.addFormData.schedule) {
				this.work_info = this.addFormData.schedule;
			}
			
			console.log('工作计划', this.addFormData);
		},
		// 文件上传
		upLoadChange(data) {
			console.log('文件列表', data);
			this.submitImgList = data;
		},
		// 删除工作
		deleteUser(index) {
			if(!this.isSubmit){
				return
			}
			this.work_info.splice(index, 1);
		},
		//添加安排
		addUser() {
			this.work_info.push({
				 workInfo: '',
				        startTime: '',
				        endTime: '',
			});
		},
		/* 设置时间 */
		setDate2(e, item, index) {
			if(!this.isSubmit){
				return
			}
			this.work_info[index].endTime = e.detail.value;
		},
		/* 设置时间 */
		setDate1(e, item, index) {
			if(!this.isSubmit){
				return
			}
			// this.work_info[index].startTime = e.detail.value
			// this.$set(要更改数据的对象,  更改的属性名 , 更改的数据)
			this.$set(this.work_info[index], 'startTime', e.detail.value);
		},
		// 点击右下文件按钮
		fbtnClick() {
			console.log('22');
		},
		/*
			点击返回
			 */
		back() {
			// uni.navigateBack({
			// 	delta: 1
			// });
			uni.redirectTo({
				url:'/packageProject/pages/project/projectManagement'
			})
		}
	}
};
</script>

<style lang="scss">
	.bottom2 {
		width: 100%;
	}
	.bottom_btn2 {
		width: 34%;
		height: 88rpx;
		border-radius: 0;
		position: fixed;
		bottom: 0;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #ffffff;
		z-index: 999;
	}
	
.line2 {
	display: flex;
	flex-direction: row;
	font-size: 24rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #333333;
}
.line1 {
	display: flex;
	flex-direction: row;
	background: #f7f7f7;
	margin-top: 34rpx;
	font-size: 26rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #333333;
}
.column1 {
	width: 240rpx;
	height: 52rpx;
	line-height: 52rpx;
	text-align: center;
	border: 1rpx solid #ebebeb;
}
.column2 {
	width: 170rpx;
	height: 52rpx;
	line-height: 52rpx;
	text-align: center;
	border: 1rpx solid #ebebeb;
}
.column3 {
	width: 110rpx;
	height: 52rpx;
	line-height: 52rpx;
	text-align: center;
	border: 1rpx solid #ebebeb;
}
.piker_date {
	width: 177rpx;
	height: 52rpx;
}

.risk_info {
	font-size: 24rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #666666;
	margin-top: 28rpx;
	display: flex;
	flex-direction: row;
	align-items: center;
}

.fbtn {
	position: fixed;
	left: 592rpx;
	bottom: 120rpx;
	z-index: 5;

	image {
		width: 134rpx;
		height: 134rpx;
	}
}

.bottom {
	width: 100%;
	// height: 88rpx;
	// margin-top: 202rpx;
	z-index: 9119;
	position: fixed;
	bottom: 0;
}

.bottom_btn {
	width: 375rpx;
	height: 88rpx;
	margin-left: 0;
	border-radius: 0;
	font-size: 32rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #ffffff;
}

.texta {
	width: 690rpx;
	height: 225rpx;
	background: #f7f7f7;
	margin-top: 30rpx;
	padding-top: 20rpx;
	padding-bottom: 20rpx;
}

.info_item {
	width: 690rpx;
	height: auto;
	border-bottom: 1rpx solid #e3e3e3;
	margin-left: 30rpx;
	margin-top: 44rpx;
}

.head_text {
	width: 100%;
	height: 128rpx;
	font-size: 36rpx;
	font-family: PingFang SC;
	font-weight: bold;
	color: #333333;
	line-height: 160rpx;
	text-align: center;
}
.add_user {
	width: 170rpx;
	height: 54rpx;
	background: #40a0f2;
	border-radius: 27rpx;
	font-size: 24rpx;
	font-family: PingFang SC;
	font-weight: bold;
	color: #ffffff;
	line-height: 54rpx;
	text-align: center;
	margin-left: 330rpx;
}
.xiaolu {
	width: 100%;
	height: 100%;
	position: fixed;
	top: 50rpx;
	z-index: 1500;
}
</style>
